/*
 * EasyPlayerVideoPlayer.cpp
 *
 *  Created on: Apr 21, 2010
 *      Author: songyp
 */

#include "EasyPlayerVideoPlayer.h"

CEasyPlayerVideoPlayer * CEasyPlayerVideoPlayer::NewL( RWsSession& aWs,
		  CWsScreenDevice& aScreenDevice,
		  RWindowBase& aWindow,
		  const TRect& aScreenRect,
		  const TRect& aClipRect )
	{
	CEasyPlayerVideoPlayer * self = CEasyPlayerVideoPlayer::NewLC( aWs,
			  aScreenDevice, aWindow, aScreenRect, aClipRect );
	CleanupStack::Pop();
	return self;
	}

CEasyPlayerVideoPlayer * CEasyPlayerVideoPlayer::NewLC( RWsSession& aWs,
		  CWsScreenDevice& aScreenDevice,
		  RWindowBase& aWindow,
		  const TRect& aScreenRect,
		  const TRect& aClipRect )
	{
	CEasyPlayerVideoPlayer * self = new (ELeave) CEasyPlayerVideoPlayer;
	CleanupStack::PushL( self );
	self->ConstructL( aWs, aScreenDevice, aWindow, aScreenRect, aClipRect );
	return self;
	}

CEasyPlayerVideoPlayer::CEasyPlayerVideoPlayer() : CActive( EPriorityStandard ),
													iState( EIdle )
	{
	
	}

void CEasyPlayerVideoPlayer::ConstructL( RWsSession& aWs,
		  CWsScreenDevice& aScreenDevice,
		  RWindowBase& aWindow,
		  const TRect& aScreenRect,
		  const TRect& aClipRect )
	{
	CActiveScheduler::Add( this );
	iPlayer = CVideoPlayerUtility::NewL( *this, EMdaPriorityNormal, 
			EMdaPriorityPreferenceNone,
			aWs, aScreenDevice, aWindow, aScreenRect, aClipRect);
	}

CEasyPlayerVideoPlayer::~CEasyPlayerVideoPlayer()
	{
	Cancel();
	delete iPlayer;
	iPlayer = NULL;
	}

void CEasyPlayerVideoPlayer::MvpuoOpenComplete( TInt aError )
	{
	iState = EOpen;
	TRequestStatus * status = &iStatus;
	User::RequestComplete( status, aError );
	SetActive();
	}

void CEasyPlayerVideoPlayer::MvpuoPrepareComplete( TInt aError )
	{
	iState = EPrepared;
	TRequestStatus * status = &iStatus;
	User::RequestComplete( status, aError );
	SetActive();
	}

void CEasyPlayerVideoPlayer::MvpuoFrameReady( CFbsBitmap& aFrame, TInt aError )
	{
	
	}

void CEasyPlayerVideoPlayer::MvpuoPlayComplete( TInt aError )
	{
	TRequestStatus * status = &iStatus;
	User::RequestComplete( status, aError );
	SetActive();

	}

void CEasyPlayerVideoPlayer::MvpuoEvent( const TMMFEvent& aEvent )
	{
	
	}

void CEasyPlayerVideoPlayer::RunL()
	{
	if( KErrNone == iStatus.Int() )
		{
		switch( iState )
			{
			case EOpen:
				iPlayer->Prepare();
				break;
			case EPrepared:
				iPlayer->Play();
				iState = EPlaying;
				break;
			case EPlaying:
				iPlayer->Close();
				iState = EIdle;
				break;
			}
		}
	else
		{
	
		}
	}

TInt CEasyPlayerVideoPlayer::RunError( TInt aError )
	{
	RDebug::Printf( "error %d", aError );
	}

void CEasyPlayerVideoPlayer::DoCancel()
	{
	iPlayer->Close();
	}

void CEasyPlayerVideoPlayer::StartPlayFileL( const TDesC& aFileName )
	{
	iPlayer->OpenFileL( aFileName );
	}
